package com.jy.servlet;

import com.alibaba.fastjson.JSON;
import com.jy.bean.Consignee;
import com.jy.service.ConsigneeService;
import com.jy.service.impl.ConsigneeSericelmpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class merchant_inquire_consigneeServlet extends HttpServlet {
    private static final Logger log = LoggerFactory.getLogger(merchant_inquire_consigneeServlet.class);
    private ConsigneeService consigneeService;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Consignee consignee = getParamesByRequest(request);

        consigneeService = new ConsigneeSericelmpl();
        List<Consignee> consignees = consigneeService.selectByParams(consignee);

        String data = JSON.toJSONString(consignees);

        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        out.println(data);
        log.debug("data:" +data);

    }

    private Consignee getParamesByRequest(HttpServletRequest request) {
        Consignee consignee = new Consignee();
        String consignee_id = request.getParameter("consignee_id");
        String consignee_name = request.getParameter("consignee_name");
        String consignee_type = request.getParameter("consignee_type");
        String consignee_price = request.getParameter("consignee_price");
        if (consignee_id != null && consignee_id.trim().length()>0){
            consignee.setId(Integer.parseInt(consignee_id));
        }
        if (consignee_name != null && consignee_name.trim().length()>0){
            consignee.setName(consignee_name);
        }
        if (consignee_type != null && consignee_type.trim().length()>0){
            consignee.setType(consignee_type);
        }
        if (consignee_price != null && consignee_price.trim().length()>0){
            consignee.setPrice(Integer.parseInt(consignee_price));
        }
        log.debug("consignee:"+consignee);
        return consignee;
    }
}
